//
//  WebServiceManager.m
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/20/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "WebServiceManager.h"
#import "RFService.h"
#import "RFRequest.h"
#import "RFResponse.h"
#import "JSONKit.h"
#import "DataManager.h"
#import "YXUtility.h"

static WebServiceManager *_sharedManager = nil;

@interface WebServiceManager(){
    NSString *userName_;
//    NSString *securityCode_;
    BOOL isnewurl;
}

@end

@implementation WebServiceManager

+ (WebServiceManager *)sharedManager {
    @synchronized( [WebServiceManager class] ){
        if(!_sharedManager)
            _sharedManager = [[self alloc] init];
        return _sharedManager;
    }
    return nil;
}

+ (id)alloc {
    @synchronized ([WebServiceManager class]){
        NSAssert(_sharedManager == nil,
                 @"Attempted to allocated a second instance");
        _sharedManager = [super alloc];
        return _sharedManager;
    }
    return nil;
}

- (id)init {
    self = [super init];
    if (self) {
        isnewurl = NO;
    }
    return self;
}

- (NSString *)baseURL{
    if (isnewurl == NO) {
        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
        NSString *url = [def stringForKey:@"url_preference"];
        NSString *port = [def stringForKey:@"port_preference"];
    
        NSArray *component = [url pathComponents];
        NSString *s1 = [component objectAtIndex:0];
        s1= [s1 stringByAppendingString:@"//"];
        NSString *s2 = [component objectAtIndex:1];
        
        NSString *s3 = [NSString stringWithFormat:@"%@:%@",s2,port];
        for (int i = 2; i < component.count; i++) {
            s3 = [s3 stringByAppendingPathComponent:[component objectAtIndex:i]];
        }
        

        NSString *address = [NSString stringWithFormat:@"http://%@/Service.aspx",s3];
       // NSLog(@"url-----------%@",[NSURL URLWithString:address]);
        return address;
    }
    else
    {
        NSString *url =  [[DataManager sharedManager]getnewServerIP];
       // NSLog(@"url-----------%@",@"http://59.57.247.24:8080/Service.aspx");
        return url;
    }
}


- (void)addParameter:(RFRequest *)request{
//    [request addParam:securityCode_ forKey:@"userSecurityCode"];
}

- (void)SyncPic:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"SyncPic" forKey:@"type"];
    [RFService execRequest:r completion:^(RFResponse *response){
        NSLog(@"%@",response);
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
    
}

- (void)checkID:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"CheckID" forKey:@"type"];
    [RFService execRequest:r completion:^(RFResponse *response){
        NSLog(@"%@",response);
        NSArray *res = [response.stringValue objectFromJSONString];
        NSDictionary *r = [res objectAtIndex:0];
        completion([r objectForKey:@"sysvalue"]);
    }];
    
}

- (void)login:(NSString *)name password:(NSString *)password u_id:(NSString *)u_id completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    
//    __block NSString *randString = [[YXUtility getInstance] getRandomString:30];
//    __block NSString *userName = name;
    
    
//
    NSString *ver = [[NSUserDefaults standardUserDefaults] objectForKey:@"order_version"];
    if (ver.length==0) {
        ver = @"85";
    }
    
    
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];
    [r addParam:[NSString stringWithFormat:@"%@",appVersion] forKey:@"c_version"];
    
    NSString *uuid = [[DataManager sharedManager]getuuid];
    if (name&&password) {
        [r addParam:name forKey:@"usercode"];
        [r addParam:password forKey:@"password"];
        [r addParam:@"Login" forKey:@"type"];
        [r addParam:ver forKey:@"OrderConfID"];
        [r addParam:@"36" forKey:@"OrderConfTypeID"];
        
        [r addParam:uuid forKey:@"MAC"];
    }
    else if(u_id)
    {
        [r addParam:u_id forKey:@"u_id"];
        [r addParam:@"Login2" forKey:@"type"];
        [r addParam:@"85" forKey:@"OrderConfID"];
        [r addParam:@"36" forKey:@"OrderConfTypeID"];
        [r addParam:uuid forKey:@"MAC"];
    }
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSLog(@"%@",response);
        [self setupUserInfo:u_id];
        id res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
    
//    [r addParam:randString forKey:@"userSecurityCode"];
    
    
}

- (RFRequest *)getPMTableRequest:(NSDate *)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
//    if (date==nil) {
//        [r addParam:@"Pm" forKey:@"type"];
//    }else{
        [r addParam:@"PZTime" forKey:@"type"];
      //  http://dwapp114.d-wolves.com:8080/3usoft/Service.aspx?type=PZTime
    if (date) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSString *formattedString = [dateFormatter stringFromDate:date];
        [r addParam:formattedString forKey:@"time"];        
    }
//    }
    return r;
}

- (void)excute:(NSString *)sqlString competion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [self addParameter:r];
    [RFService execRequest:r completion:^(RFResponse *response){
        id res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (NSString *)getUserName{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    NSString *res = [defs objectForKey:@"pm_user_name"];
    return res;
}

- (void)setupUserInfo:(NSString *)userName{
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
    [defs setObject:userName forKey:@"pm_user_name"];
    [defs synchronize];
    
//    securityCode_ = code;
}

- (void)submit:(NSString *)orderJSON completion:(WebServiceCompletion)competion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"SubmitOrder" forKey:@"type"];
    [r addParam:orderJSON forKey:@"Content"];
    
//    [self addParameter:r];
    [RFService execRequest:r completion:^(RFResponse *response){
        NSDictionary *res = [response.stringValue objectFromJSONString];
        competion(res);
    }];
}


- (RFRequest *)getProductInfoRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                         type:RFRequestMethodGet
       resourcePathComponents:nil];
    [r addParam:@"SpdmTime" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshProductInfo:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getProductInfoRequest:date];
//        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
//    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
       // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getProductColoridRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QuerySpdmDetail" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshProductColorid:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getProductColoridRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getProductPictureRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QuerySpdmImg" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshProductPicture:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getProductPictureRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getColortblRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QueryColortbl" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshColortbl:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getColortblRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getXilieRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QueryXilie" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshXilie:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getXilieRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getKuansRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QueryKuans" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshKuans:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getKuansRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getKuanxRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QueryKuanx" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshKuanx:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getKuanxRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getXiaoxlRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QueryXiaoxl" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshXiaoxl:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getXiaoxlRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getNianfRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QueryNianf" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshNianf:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getNianfRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getJijieRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QueryJijie" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshJijie:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getJijieRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getPingpRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QueryPingp" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshPingp:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getPingpRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    //    http://192.168.3.9:9501/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (RFRequest *)getSpdmStatusTimeInfoRequest:(NSDate *)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    if (date==nil) {
        [r addParam:@"SpdmStatusTime" forKey:@"type"];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        date = [dateFormatter dateFromString:@"1900-01-01 00:00:00"];
        NSString *formattedString = [dateFormatter stringFromDate:date];
        [r addParam:formattedString forKey:@"time"];
        
    }else{
        [r addParam:@"SpdmStatusTime" forKey:@"type"];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSString *formattedString = [dateFormatter stringFromDate:date];
        [r addParam:formattedString forKey:@"time"];
    }
    return r;
}

- (void)refreshSpdmStatusTimeInfo:(NSDate *)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getSpdmStatusTimeInfoRequest:date];
    //        http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmTime&time=1900-01-01%2000:00:00
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
        // NSLog(@"%@",res);
    }];
}

- (void)refreshSaleSegment:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"Querydictvalue" forKey:@"type"];
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)refreshSizeInfo:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
//    http://dwapp112.d-wolves.com:8080/3usoft/Service.aspx?type=SpdmSize
    [r addParam:@"SpdmSize" forKey:@"type"];
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (RFRequest *)getSpdmSizeTableRequest:(NSDate *)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"SpdmSize" forKey:@"type"];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *formattedString = [dateFormatter stringFromDate:date];
    [r addParam:formattedString forKey:@"time"];
    return r;
}

- (void)refreshSpdmSizeTable:(NSDate *)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getSpdmSizeTableRequest:date];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
    
}

- (RFRequest *)getg_SizeTableRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QuerySpdmSize2" forKey:@"type"];
    
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshg_SizeTable:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getg_SizeTableRequest:date];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
    
}

- (void)getServerIP:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"GetServerIP" forKey:@"type"];
    [RFService execRequest:r completion:^(RFResponse *response){
        NSDictionary *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)refreshPMTable:(NSDate *)date completion:(WebServiceCompletion)completion{//配装主表
    RFRequest *r = [self getPMTableRequest:date];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
    
    
}

- (RFRequest *)getOrderTableRequest:(long)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    ShopObject *shopobj = [[DataManager sharedManager]getCurrentShop];
    [r addParam:shopobj.shopID forKey:@"shopid"];
    [r addParam:@"OrderTime2" forKey:@"type"];
    NSString *formattedString = [NSString stringWithFormat:@"%ld",date];
    [r addParam:formattedString forKey:@"MaxSynID"];
    return r;
}

- (void)refreshOrderTable:(long)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [self getOrderTableRequest:date];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
    
}


- (RFRequest *)getPDTableRequest:(NSString *)scripno{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:scripno forKey:@"scripno"];
    [r addParam:@"Pd" forKey:@"type"];
    
    return r;
}


- (void)refreshPDTable:(NSString *)scripno completion:(WebServiceCompletion)completion{//配装主表
    if (scripno==nil) {
        return;
    }
    RFRequest *r = [self getPDTableRequest:scripno];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

//商品过滤类别表
- (void)refreshReferenceTable:(WebServiceCompletion)competion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"ReferencePara" forKey:@"type"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        competion(res);
    }];
}

- (void)refreshSizeTable:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"SizeTime" forKey:@"type"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)refreshTipsTable:(NSDate *)date completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"NoticeQuery" forKey:@"type"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}
/*
- (void)refreshg_SizeTable:(WebServiceCompletion)competion{
    RFRequest *r = [RFRequest requestWithURL:[self baseURL]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QuerySpdmSize2" forKey:@"type"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        competion(res);
    }];
}
*/
- (void)getShopOrderList:(NSString *)usercode completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"OrderQuery" forKey:@"type"];
    [r addParam:usercode forKey:@"usercode"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)getShopList:(NSString *)usercode completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"QueryShop" forKey:@"type"];
    [r addParam:usercode forKey:@"usercode"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSLog(@"%@",response);
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)checkShop:(NSString *)shopid completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"IPADCheckCustOrder" forKey:@"type"];
    [r addParam:shopid forKey:@"shopid"];
  //  [r addParam:usercode forKey:@"usercode"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)VeriShop:(NSString *)usercode password:(NSString *)password shopid:(NSString *)shopid completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    
  //  int usercodenum = [usercode intValue];
    [r addParam:@"IPADVeriOrderCust" forKey:@"type"];
    [r addParam:usercode forKey:@"usercode"];
    [r addParam:password forKey:@"password"];
    [r addParam:shopid forKey:@"shopid"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)VeriShop:(NSString *)u_id shopid:(NSString *)shopid completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    
    //  int usercodenum = [usercode intValue];
    [r addParam:@"IPADVeriOrderCust2" forKey:@"type"];
    [r addParam:u_id forKey:@"u_id"];
    [r addParam:shopid forKey:@"shopid"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)getServerReport:(NSString *)usercode
                 ShopID:(NSString*)shopid
                    SKU:(NSString*)sku
              QueryType:(NSString*)queryType
             completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"AllOrderQuery" forKey:@"type"];
    [r addParam:usercode forKey:@"UserCode"];
    [r addParam:shopid forKey:@"ShopID"];
    [r addParam:sku forKey:@"SKU"];
    [r addParam:queryType forKey:@"QueryType"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)setNewPassword:(NSString *)usercode password:(NSString *)password completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"ReSetPW" forKey:@"type"];
    [r addParam:usercode forKey:@"usercode"];
    [r addParam:password forKey:@"password"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSString *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];

}

-(void)checkShopTotal:(NSString*)shopid count:(NSString *)count money:(NSString*)money completion:(WebServiceCompletion)competion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"CheckDatashop" forKey:@"type"];
    [r addParam:count forKey:@"totalcount"];
    [r addParam:money forKey:@"totalmoney"];
    [r addParam:shopid forKey:@"shopid"];
    
    //    [self addParameter:r];
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        competion(res);
    }];
}

- (void)checktotal:(NSString*)usercode count:(NSString *)count money:(NSString*)money completion:(WebServiceCompletion)competion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"CheckData" forKey:@"type"];
    [r addParam:count forKey:@"totalcount"];
    [r addParam:money forKey:@"totalmoney"];
    [r addParam:usercode forKey:@"usercode"];
    
    //    [self addParameter:r];
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        competion(res);
    }];
}

-(void)setnewServerIP:(BOOL)sender
{
    isnewurl = sender;
}

- (void)gettip:(NSString *)usercode time:(NSString *)time completion:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"NoticeQuery" forKey:@"type"];
    [r addParam:usercode forKey:@"usercode"];
    [r addParam:time forKey:@"time"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (RFRequest *)getTipRequest:(NSString *)usercode date:(NSString *)date{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    if (date==nil) {
        [r addParam:@"NoticeQuery" forKey:@"type"];
        [r addParam:usercode forKey:@"usercode"];
        [r addParam:@"1900-01-01 00:00:00" forKey:@"time"];
    }else{
        [r addParam:@"NoticeQuery" forKey:@"type"];
        [r addParam:usercode forKey:@"usercode"];
        
//        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//        NSString *formattedString = [dateFormatter stringFromDate:date];
        [r addParam:date forKey:@"time"];
    }
    return r;
}

- (void)getSpdmOrder:(WebServiceCompletion)completion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"SpdmOrder" forKey:@"type"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)forceIncreaseUpdateOrder:(WebServiceCompletion)completion shop:(NSString *)shopid orders:(NSString*)orders user:(NSString*)user{
    NSURL *url = [NSURL URLWithString:[self baseURL]];
    NSString *urlString = url.absoluteString;
    //    urlString = [NSString stringWithFormat:@"%@?type=%@&shopid=%@&usercode=%@",urlString,@"UpdateAllBill",shopid,user];
    
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:urlString]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:shopid forKey:@"shopid"];
    [r addParam:orders forKey:@"bill"];
    [r addParam:@"UpdateAddBill" forKey:@"type"];
    [r addParam:user forKey:@"usercode"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        
        if (response.stringValue==nil) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:@"1" forKey:@"ioresult"];
            [dic setObject:@"网络链接错误，请检查您的网络设置" forKey:@"Message"];
            completion(dic);
        }else{
            NSArray *res = [response.stringValue objectFromJSONString];
            completion(res);
        }
        
    }];
}


- (void)forceUpdateOrder:(WebServiceCompletion)completion shop:(NSString *)shopid orders:(NSString*)orders user:(NSString*)user{
    NSURL *url = [NSURL URLWithString:[self baseURL]];
    NSString *urlString = url.absoluteString;
//    urlString = [NSString stringWithFormat:@"%@?type=%@&shopid=%@&usercode=%@",urlString,@"UpdateAllBill",shopid,user];
    
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:urlString]
                                        type:RFRequestMethodPost
                      resourcePathComponents:nil];
    [r addParam:shopid forKey:@"shopid"];
    [r addParam:orders forKey:@"bill"];
    [r addParam:@"UpdateAllBill" forKey:@"type"];
    [r addParam:user forKey:@"usercode"];
    
    [RFService execRequest:r completion:^(RFResponse *response){
        NSArray *res = [response.stringValue objectFromJSONString];
        completion(res);
    }];
}

- (void)cleardeviceid:(NSString *)usercode completion:(WebServiceCompletion)competion{
    RFRequest *r = [RFRequest requestWithURL:[NSURL URLWithString:[self baseURL]]
                                        type:RFRequestMethodGet
                      resourcePathComponents:nil];
    [r addParam:@"ClearDeviceID" forKey:@"type"];
    [r addParam:usercode forKey:@"usercode"];
    
    //    [self addParameter:r];
    [RFService execRequest:r completion:^(RFResponse *response){
        NSDictionary *res = [response.stringValue objectFromJSONString];
        competion(res);
    }];
}

@end
